﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Open_MUD.Kernel.Utils
{
    class Util
    {
        private static Util __instance;

        public static Util getInstance()
        {
            if (__instance == null)
                __instance = new Util();
            return __instance;
        }

        public String csvList(String[] list)
        {
            String ret = "";
            int i = 1;
            foreach (String item in list)
            {
                ret += item + (list.Length != i++ ? ", " : "");
            }
            return ret;
        }

        public String formatList(String[] list)
        {
            String ret = "";
            int i = 1;
            foreach (String item in list)
            {
                ret += i + ".- " + item + (list.Length != i?"%^RET%^":"");
                i++;
            }
            return ret;
        }

        public string GetMD5Hash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string password = s.ToString();
            return password;
        }
        
        public string FormatColorNumber(int num)
        {
           if ( num >= 0 )
              return "%^GREEN%^" + num;
           else if ( num < 0 )
              return "%^RED%^" + num;
           return "" + num;
        }
        
        public string FormatColorNumber(String num)
        {
           return FormatColorNumber(System.Int32.Parse(num)); 
        }

        internal bool EmailCorrecto(string line)
        {
            int arroba = line.IndexOf("@");
            if (arroba == 0 || arroba == -1 || arroba >= line.Length - 2)
                return false;
            return true;
        }
    }
}
